// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
  env: 'cloud1-cloud1-7gb9s3y025614beb'
})
// 云函数入口函数
exports.main = async (event, context) => {
  const db = cloud.database()
  const pics = db.collection('pics')
  const votes = db.collection('votes')
  const $ = db.command.aggregate
  let res = await pics.get()
  let plist = res.data
  res = await votes.aggregate().group({
    _id: '$fileid',
    count: $.sum(1)
  }).end()
  console.log(res)
  let vlist = res.list
  plist.map(v => {
    let found = vlist.find(vv => {
      return vv._id == v.fileid
    })
    if (found) {
      v.count = found.count
    } else {
      v.count = 0
    }
  })
  return {
    plist
  }
}